package com.archerdu.apply.controller.front;

import javax.servlet.http.HttpServletRequest;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.archerdu.apply.model.Member;
import com.archerdu.apply.service.MemberManager;
import com.archerdu.apply.service.RoleManager;
import com.archerdu.base.controller.BaseController;
import com.archerdu.commons.constant.DataConstant;
import com.archerdu.commons.util.ServletUtil;

/**
 * 
 * @projectName springDemo
 * @description 会员类
 * @author archer du
 * @createDate 2013年10月29日 下午1:35:51
 * @updateMember dxf
 * @updateDate 2013年10月29日 下午1:35:51
 * @updateRemark 新建
 * @version 0.0.1
 */
@Controller("frontMember")
@Scope("prototype")
@RequestMapping("/member")
public class MemberController extends BaseController {

    private static final long serialVersionUID = -6777950424131138768L;

    private MemberManager     memberManager;

    private RoleManager       roleManager;

    private Member            member;
    
    /**
     * 注册页面
     * 
     * @param requ
     * @param uc
     * @return
     */
    @RequestMapping(value = "/register", method=RequestMethod.GET)
    public ModelAndView registerPage(HttpServletRequest requ, MemberController uc) {
        ModelAndView mav = new ModelAndView();
        mav.setViewName("front/member/register");
        return mav;
    }
    
    /**
     * 注册
     * 
     * @param requ
     * @param uc
     * @return
     */
    @RequestMapping(value = "/register", method=RequestMethod.POST)
    public ModelAndView register(HttpServletRequest requ, MemberController uc) {
        ModelAndView mav = new ModelAndView();
        member = uc.getMember();

        if ("post".equalsIgnoreCase(requ.getMethod())) {
            try {
                member.setFlag(DataConstant.USER_FLAG_SYS);
                member.setRegIp(ServletUtil.getClientIpAddr(requ));
                member.setRegtime(getSqlDate());
                member.setUseFlag(DataConstant.COMMON_USE_FLAG_YES);
                memberManager.add(member);
                addMessage(DataConstant.COMMON_ADD_SUCCESS);
                mav.setViewName("front/member/registerSuccess");
            } catch (Exception e) {
                addMessage(DataConstant.COMMON_ADD_FAILED);
                mav.setViewName("front/member/register");
                logger.error(e.getMessage(), e);
            }
        }
        mav.addObject(getMessage());
        return mav;
    }

    /**
     * 用户编辑
     * 
     * @param requ
     * @param uc
     * @return
     */
    @RequestMapping(value = "/edit")
    public ModelAndView edit(HttpServletRequest requ, MemberController uc) {
        logger.debug("uc.getMember() is " + uc.getMember());
        member = uc.getMember();
        ModelAndView mav = new ModelAndView();
        if ("get".equalsIgnoreCase(requ.getMethod())) {
            if (!StringUtils.isEmpty(member.getId())) {
                member = memberManager.get(member.getId());
                mav.addObject(member);
            }
        }

        mav.setViewName("front/member/edit");
        return mav;
    }

    public void setMemberManager(MemberManager memberManager) {
        this.memberManager = memberManager;
    }

    public Member getMember() {
        return member;
    }

    public void setMember(Member member) {
        this.member = member;
    }

    public RoleManager getRoleManager() {
        return roleManager;
    }

    public void setRoleManager(RoleManager roleManager) {
        this.roleManager = roleManager;
    }

    public MemberManager getMemberManager() {
        return memberManager;
    }
}
